package com.zdj.question;

/**
 * @author zdj
 * @date 2024-06-14 12:00
 */
//hide-start
public class PrimeNumber {

    public static void main(String[] args) {
        Integer n = Integer.valueOf(args[0]);
        System.out.println(PrimeNumberSolution.primeNumberSolution(n));
    }
}
//hide-end
class PrimeNumberSolution {
    public static String primeNumberSolution(int n) {
        // 编码区
        StringBuilder result = new StringBuilder();
        // 初始化所有数字为质数
        boolean[] isPrime = new boolean[n + 1];

        // 假设所有数字都是质数，除了0和1
        for (int i = 2; i <= n; i++) {
            isPrime[i] = true;
        }

        // 标记非质数
        for (int i = 2; i * i <= n; i++) {
            if (isPrime[i]) {
                for (int j = i * i; j <= n; j += i) {
                    isPrime[j] = false;
                }
            }
        }

        // 收集质数
        for (int i = 2; i <= n; i++) {
            if (isPrime[i]) {
                if (result.length() > 0) {
                    result.append(",");
                }
                result.append(i);
            }
        }

        return result.toString();
    }
}
